package com.kuaifu.autofare.common.utils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;


public class XMLUtil {
	
	public static void parseElement(Element elt,Map<String,String> map) {
		if(elt.getText() != null && !"".equals((String)elt.getText())) {
			map.put(elt.getName(), (String)elt.getText());
		}
		
		if(elt.getChildren().size() == 0) return;
		
		Iterator<?> iter = elt.getChildren().iterator();
		while(iter.hasNext()) {
			Element childElt = (Element)iter.next();
			parseElement(childElt,map);
		}
	}
	
	public static Map<String,String> parseXmlFromInputStream(InputStream is) throws Exception{
		Map<String,String> map = new HashMap<String, String>();
		Document doc = null;
		SAXBuilder sb = new SAXBuilder();
		doc = sb.build(is);
		Element rootElt = doc.getRootElement(); // 获取根节点
		parseElement(rootElt,map);
		return map;
	}
}
